package DataOperator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class BasePackager {
	static final String mainSoundDir = "../voice/";
	static final String backSoundDir = "../sound/";
	static final String multSoundDir = "../multi/";
	
	static final String OutputDir = "Out/";
	static final String LogFilePath = OutputDir + "BookLog.txt";
	
	protected Builder builder = null;
	protected ArrayList<String> noTransWords = new ArrayList<String>();
	protected ArrayList<String> noSoundWords = new ArrayList<String>();
	
	protected void Init() {
	}
	
	// package global info here
	protected void BeforeBuild() throws IOException{
		noTransWords.clear();
		noSoundWords.clear();
	}
	
	protected void AfterBuild(){
		try{
			BufferedWriter log = new BufferedWriter(new FileWriter(LogFilePath));
			log.write("no trans word\r\n");
			for(String s : noTransWords){
				log.write(s + "\r\n");
			}
			log.write("no sound word\r\n");
			for(String s : noSoundWords){
				log.write(s + "\r\n");
			}
			log.close();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public void PrepareWord(String word, WordBuildInfo wordInfo) throws IOException{
		PrepareSoundInfo(word, wordInfo);
	}
	
	public void BuildWord(String word, WordBuildInfo info) throws IOException{
		
	}
	
	public void PrepareSoundInfo(String word, WordBuildInfo wordInfo){
		wordInfo.soundFilePath = "";
		wordInfo.soundFileLength = 0;
		char key = word.charAt(0);
		assert(key >= 'A' && key <= 'Z' || key >= 'a' && key <= 'z');
		String filePath = mainSoundDir + key + "/" + word + ".wav";
		File file = new File(filePath);
		if(!file.exists()){
			filePath = backSoundDir + key + "/" + word + ".mp3";
			file = new File(filePath);
			if(!file.exists()){
				return;
			}
		}
		wordInfo.soundFilePath = filePath;
		wordInfo.soundFileLength = (int)file.length();
	}
}
